home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / examples / smoothing / ex1.d next >
Encoding:
Text File  |  1991-03-10  |  3.7 KB  |  115 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. /* This program performs a horizontal smoothing, with one row of the matrix
  3.  * per processor.  The matrix is of size M x N.  Each iteration of the
  4.  * algorithm sets row i to be the average of rows i-1 and i+1, except for
  5.  * rows on the edge of the matrix, which are left constant. */
  6.  
  7. #include "dino.h"
  8.  
  9. #define max(x,y) (x > y ? x : y)
  10. #define min(x,y) (x < y ? x : y)
  11.  
  12. #define M 16
  13. #define N 11
  14.  
  15. environment node[M:id] {
  16.  
  17.   composite smooth (a, in iter)
  18.  
  19.                                 /* ==> Since no "in" or "out" keyword has been
  20.                                        used before a, it is an in/out
  21.                                        parameter. */
  22.  
  23.   double distributed a[M][N] map BlockRowOverlap;
  24.  
  25.   int iter;                     /* ==> Illustrates use of a non-distributed
  26.                                        parameter to a composite procedure.
  27.                                        Note the use of the "in" keyword in
  28.                                        the parameter list. */
  29.  
  30.   {
  31.     int i, j;           /* Looping variables */
  32.  
  33.     /* Repeat the smoothing process iter times */
  34.     for (i = 0; i < iter; i++) {
  35.  
  36.       /* Send out your data and receive it back again */
  37.  
  38.       if (i != 0) {             /* ==> We don't need to communicate on the
  39.                                        first iteration, because the data we
  40.                                        need has been set up by the composite
  41.                                        procedure call. */
  42.  
  43.         a[<id,id>][]# = a[<id,id>][];
  44.  
  45.                                 /* ==> Implicit send of the id'th row of a.
  46.                                        Notice the use of the # sign, and how
  47.                                        an assignment statement is used to
  48.                                        send data. */
  49.  
  50.                                 /* ==> Used a[<id,id>][]# instead
  51.                                        of a[id][]# because of a bug in DINO. */
  52.  
  53.         a[<max(id-1,0),min(id+1,M-1)>][]#;
  54.  
  55.                                 /* ==> Implicit receive.  Receives rows id-1
  56.                                        and id+1, except on the edges, where
  57.                                        it only receives one row. */
  58.  
  59.       }
  60.  
  61.       /* Perform the computation, but only on non-edge nodes */
  62.       if (id != 0 && id != M - 1)
  63.         for (j = 0; j < N; j++)
  64.           a[id][j] = (a[id-1][j] + a[id+1][j]) / 2;
  65.  
  66.     }
  67.   }
  68. }
  69.  
  70. environment host {
  71.  
  72.   void main ()
  73.  
  74.   {
  75.     double a[M][N];                     /* Input data */
  76.     int iter;                           /* Holds the iteration count */
  77.  
  78.     int i, j;                           /* Looping variables */
  79.  
  80.     /* Set up the initial data for a[][] */
  81.     for (i = 0; i < M; i++)
  82.       for (j = 0; j < N; j++)
  83.         a[i][j] = (i + 1)*(j + 1);
  84.     for (i = 1; i < M - 1; i++)
  85.       for (j = 0; j < N; j++)
  86.         a[i][j] = 0;
  87.  
  88.     iter = 300;                 /* ==> We must use a variable to hold the
  89.                                        number of iterations, because of a
  90.                                        bug in DINO which doesn't allow
  91.                                        passing constants to a composite
  92.                                        procedure. */
  93.  
  94.     /* Print out the initial data */
  95.     printf ("Initial data for a:\n");
  96.     for (i = 0; i < M; i++) {
  97.       for (j = 0; j < N; j++)
  98.         printf ("%6.2f", a[i][j]);
  99.       printf ("\n");
  100.     }
  101.  
  102.     /* Perform the computation */
  103.     smooth (a[][], iter)#;
  104.  
  105.     /* Printout the results */
  106.     printf ("Result data for a:\n");
  107.     for (i = 0; i < M; i++) {
  108.       for (j = 0; j < N; j++)
  109.         printf ("%6.2f", a[i][j]);
  110.       printf ("\n");
  111.     }
  112.   }
  113. }
  114.  
  115.